#!/bin/bash
cd ~/app-root/data/temp/hosts
curl -1 "https://app.arukas.io/api/login" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0" --data "email=pigoig"%"40gmail.com&password=dcs38UR4kEOcybqZ&mfa_token=" -D log.txt 2>/dev/null
cookie="$(grep 'Set-Cookie:' log.txt|cut -d\  -f2|tr '\n' ' '|sed 's#; *$##')"
curl -1 -n -s https://app.arukas.io/api/containers -H "Content-Type: application/vnd.api+json" -H "Accept: application/vnd.api+json" -u 2de28ea9-72b6-4ff6-a64c-59a393a3d059:UmIreyyWAR5fu7tluRCHdyRWt1ozEk8KjRTidiZzYMsAoyQ8DwDTcHfyRts8W1Fr|grep -o 'app_id\":\"[a-zA-Z0-9\-]*'|cut -d\" -f3|sort -u >result.txt
for i in result.txt;do
    curl -1 https://app.arukas.io/api/capacity --Cookie "$cookie"
    sleep 1
    curl -1 https://app.arukas.io/api/containers/$i --Cookie "$cookie" 2>/dev/null
    sleep 1
    curl -1 https://app.arukas.io/api/containers/$i --Cookie "$cookie" 2>/dev/null
    sleep 1
done
curl -1 -n -s https://app.arukas.io/api/containers -H "Content-Type: application/vnd.api+json" -H "Accept: application/vnd.api+json" -u 2de28ea9-72b6-4ff6-a64c-59a393a3d059:UmIreyyWAR5fu7tluRCHdyRWt1ozEk8KjRTidiZzYMsAoyQ8DwDTcHfyRts8W1Fr|sed 's#},{#\n#g' |grep 'shadowsocksr'|grep "\-[0-9]\{1,3\}\-[0-9]\{1,3\}\-[0-9]\{1,3\}\-[0-9]\{1,3\}\.">result.txt
if ! [ -s "result.txt" ]; then rm -f result.txt;exit;fi
s=$(sed -n '/$/=' result.txt|sed -n '$p')
for((i=1;i<=$s;i++)); do
port=$(cat result.txt|sed -n "${i}p"|grep -o 'container_port\":[a-z0-9\"\,\._:-]*'|grep -o '[0-9]\{5\}'|tail -1)
ip=$(cat result.txt|sed -n "${i}p"|grep -o 'container_port\":[a-z0-9\"\,\._:-]*'|grep -o '[0-9]\{1,3\}-[0-9]\{1,3\}-[0-9]\{1,3\}-[0-9]\{1,3\}'|tail -1|sed 's#-#.#g')
pwd=$(cat result.txt|sed -n "${i}p"|grep -o ' -k [a-zA-Z0-9_\.\-]\{1,\}'|cut -d\  -f3)
method=$(cat result.txt|sed -n "${i}p"|grep -o ' -m [a-zA-Z0-9_\.\-]\{1,\}'|cut -d\  -f3)
protocol=$(cat result.txt|sed -n "${i}p"|grep -o ' -O [a-zA-Z0-9_\.\-]\{1,\}'|cut -d\  -f3)
obfs=$(cat result.txt|sed -n "${i}p"|grep -o ' -o [a-zA-Z0-9_\.\-]\{1,\}'|cut -d\  -f3)
cat >  ~/app-root/repo/hosts/ss${i}.txt<<-EOF
{
    "server":"$ip",
    "server_port":$port,
    "local_address": "0.0.0.0",
    "local_port": 1080,
    "password":"$pwd",
    "method":"$method",
    "protocol":"$protocol",
    "obfs":"$obfs"
}
EOF
done
rm -f ~/app-root/repo/hosts/ss[$(($s+1))-9].txt result.txt log.txt
gop=$(curl -1 -n -s https://app.arukas.io/api/containers -H "Content-Type: application/vnd.api+json" -H "Accept: application/vnd.api+json" -u 2de28ea9-72b6-4ff6-a64c-59a393a3d059:UmIreyyWAR5fu7tluRCHdyRWt1ozEk8KjRTidiZzYMsAoyQ8DwDTcHfyRts8W1Fr|sed 's#},{#\n#g' |grep -o '_port\":8443[a-zA-Z0-9.,;:_"-]*.arukascloud.io')
port=$(echo $gop|grep -o '[0-9]\{5\}'|tail -1)
if [ -n "$port" ]; then
  if ! grep -q $port ~/app-root/runtime/repo/list/proxy.pac; then
    #sed -i "/ming92/s#:[0-9]*#:$port#" ~/app-root/runtime/repo/list/proxy.pac
    DP_TOKEN="$(cat ~/app-root/data/temp/tool/dnspod.txt)"
    _domain="ming92.tk"
    _sub_domain="go1"
    _record_type="A"
    _record_value="$(echo $gop|grep -o '[0-9]\{1,3\}-[0-9]\{1,3\}-[0-9]\{1,3\}-[0-9]\{1,3\}'|sed 's#-#.#g'|tail -1)"
    _record_id=$(curl -s -X POST "https://dnsapi.cn/Record.List" -d "login_token=$DP_TOKEN&format=json&domain=$_domain&sub_domain=$_sub_domain"|grep -o 'records".*id.*'|grep -o "[0-9]\{5,\}")
    result=$(curl -s -X POST "https://dnsapi.cn/Record.Modify" -d "login_token=$DP_TOKEN&format=json&domain=$_domain&record_id=$_record_id&sub_domain=$_sub_domain&record_type=$_record_type&record_line=默认&value=$_record_value")
    if ! echo $result|grep -q "$_record_value"; then
        result=$(curl -s -X POST "https://dnsapi.cn/Record.Modify" -d "login_token=$DP_TOKEN&format=json&domain=$_domain&record_id=$_record_id&sub_domain=$_sub_domain&record_type=$_record_type&record_line=默认&value=$_record_value")
    fi
    sed -i "/ming92/s#[0-9]\{3,\}#$port#" ~/app-root/repo/list/pac.txt
    cd ~/app-root/data/github-sh/tool/gfw-pac/
    python ./gfw-pac.py -f ~/app-root/repo/list/proxy.pac -p "HTTPS go1.ming92.tk:$port"
  fi
fi
sni=$(curl -1 -n -s https://app.arukas.io/api/containers -H "Content-Type: application/vnd.api+json" -H "Accept: application/vnd.api+json" -u 2de28ea9-72b6-4ff6-a64c-59a393a3d059:UmIreyyWAR5fu7tluRCHdyRWt1ozEk8KjRTidiZzYMsAoyQ8DwDTcHfyRts8W1Fr|sed 's#},{#\n#g' |grep -o '_port\":443[a-zA-Z0-9.,;:_"-]*.arukascloud.io')
port=$(echo $sni|grep -o '[0-9]\{5\}'|tail -1)
if [ -n "$port" ]; then
    ip=$(echo $sni|grep -o '[0-9]\{1,3\}-[0-9]\{1,3\}-[0-9]\{1,3\}-[0-9]\{1,3\}'|sed 's#-#.#g'|tail -1)
    #if ! grep -q "$ip:$port" ~/app-root/data/github-sh/ad/my7.txt;then
    #    sed -i "/443/s#[0-9\.]\{1,\}:[0-9]\{3,5\}#$ip:$port#" ~/app-root/data/github-sh/ad/my7.txt
    #fi
fi